package com.example.basicknowledge.各种按钮;

import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.basicknowledge.R;

import java.nio.charset.CharsetEncoder;

public class Buttons extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private Button button;
    private ImageButton imageButton;
    private CheckBox checkBox;
    private RadioGroup radioGroup;
    private ToggleButton toggleButton;
    private Switch switchButton;
    private FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buttons);

        //用buttonOnClick方法实现了
//        button = (Button)findViewById(R.id.button5);
//        button.setOnClickListener(this);

        imageButton = (ImageButton)findViewById(R.id.imageButton);
        imageButton.setOnClickListener(this);

        checkBox = (CheckBox)findViewById(R.id.checkBox);
        checkBox.setOnClickListener(this);

        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);

        toggleButton = (ToggleButton)findViewById(R.id.toggleButton);
        toggleButton.setOnClickListener(this);

        switchButton = (Switch)findViewById(R.id.switch1);
        switchButton.setOnClickListener(this);

        floatingActionButton = (FloatingActionButton)findViewById(R.id.floatingActionButton);
        floatingActionButton.setOnClickListener(this);
    }

    public void buttonOnClick(View view){
        //android:onClick="buttonOnClick" 在xml文件中如此设置，点击button后就会调用此方法
        onClick(view);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button5:
            case R.id.imageButton:
            case R.id.floatingActionButton:
                Toast.makeText(this,"点击了" + view.getClass().toString(),Toast.LENGTH_SHORT).show();
                break;
            case R.id.checkBox:
                Toast.makeText(this,"点击了checkBox isCheck = " + checkBox.isChecked(),Toast.LENGTH_SHORT).show();
                break;
            case R.id.toggleButton:
                Toast.makeText(this,"点击了toggleButton isCheck = " + toggleButton.isChecked(),Toast.LENGTH_SHORT).show();
                break;
            case R.id.switch1:
                Toast.makeText(this,"点击了switch isCheck = " + switchButton.isChecked(),Toast.LENGTH_SHORT).show();
                break;
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(this,"点击了radioGroup  " + radioButton.getText(),Toast.LENGTH_SHORT)
                .show();
    }
}
